package com.situ.mall.controller.front;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Cart;
import com.situ.mall.entity.Result;
import com.situ.mall.entity.User;
import com.situ.mall.service.ICartService;

@Controller
@RequestMapping("/cart")
public class FrontCartController {
	
	@Autowired
	private ICartService cartService;
	
//前台 
	// 1.product_detail.jsp  加入购物车（插入cart表）
	@RequestMapping("/insertCart.shtml")
	@ResponseBody
	public Result insertCart(Cart cart/* , HttpSession session */) {
		System.out.println("FrontCartController.insertCart()");
		System.out.println(cart);
		
//		User user = (User)session.getAttribute("user");
//		cart.setUserId(user.getId());
//		cart.setChecked(1);
		
		boolean isSuccess = cartService.insertCart(cart);
		
		if (isSuccess) {
			return Result.success("加入购物车成功");
		}else {
			return Result.error("加入购物车失败");
		}
	}
	
	// 2. 2020.03.20：商品集合 跳转 商品详情： product_list.jsp -->  点我的购物车(根据 userId 用户id) --> cart_list.jsp购物车页面 所有商品  (cart表里的全部内容)
	@RequestMapping("/getCartListJsp.shtml")
	public String  getCartListJsp(Integer userId ,Model model ) {
		System.out.println("FrontCartController.getCartListJsp()");
		System.out.println(userId);
		
		/*   遍历 购物车  */
		
		List<Cart> cartList = cartService.selectCartListByUserId(userId);
		
		model.addAttribute("cartList", cartList);
		
		return "cart_list";
		
	}

	// 3. 2020.03.20：:删除 购物车的商品   （删除cart表  by cartId）（deleteCartItemByCartId）  
	@RequestMapping("/deleteCartItemByCartId")
	@ResponseBody
	public Result deleteCartItemByCartId(Integer cartId) {
		System.out.println("FrontCartController.deleteCartItemByCartId()");
		System.out.println(cartId);
		
		boolean isSuccess = cartService.deleteCartItemByCartId(cartId);
		
		if (isSuccess) {
			return Result.success("购物车的商品删除成功");
		} else {
			return Result.error("购物车的商品删除失败");
		}	
	}
	
	// 4. 2020.03.20  修改 购物车内 商品的数量（update cart表的quantity字段）  
	@RequestMapping("/updateCartItemQuantityByCartId")
	@ResponseBody
	public Result updateCartItemQuantityByCartId(Integer cartId, Integer addOrSub) {
		System.out.println("FrontCartController.updateCartItemQuantityByCartId()");
		System.out.println("addOrSub :"+addOrSub+"，addOrSub类型："+addOrSub.getClass().toString());
		
		boolean isSuccess = cartService.updateCartItemQuantityByCartId(cartId, addOrSub);
		
		if (isSuccess) {
			return Result.success("商品数量 修改成功");
		} else {
			return Result.error("商品数量 修改失败");
		}
	}
	
	//5.购物车页面 cart_list.jsp :复选框  选中checked=1，未选中checked=0
	@RequestMapping("/updateCartItemCheckStatusByCartId")
	@ResponseBody
	public Result updateCartItemCheckStatusByCartId(Integer cartId, Integer checked) {
		System.out.println("FrontCartController.updateCartItemCheckStatusByCartId()");
		System.out.println("cartId："+cartId+",checked："+checked);
		
		boolean isSuccess = cartService.updateCartItemCheckStatusByCartId(cartId,checked);
		
		if (isSuccess) {
			return Result.success("多选框更新成功");
		} else {
			return Result.error("多选框更新失败");
		}
		
	}
	
	
}
